<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date"%>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${ctx}/static/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="${ctx}/static/agent/floorInfo.js?v=<%=new Date().getTime() %>"/>
    <script type="text/javascript">

        var ctx = "${ctx}";
        //查看
        function show(orderId) {
            layer.open({
                type:1,
                title:"查看",
                skin:"myclass",
                area:["500px","300px"],
                content:$("#process_00").html(),
                success:function (layero,index) {
                    var form = layui.form;
                    form.render();
                    layero.addClass('layui-form');
                    layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
                    getRepaireInfo(layero,'/repaireInfo/getRepaireInfoByOrderId',null,orderId);

                },
                btn:['关闭'],
                yes:function (index,layero) {
                    layer.close(index);
                }
            });
        }
        //流转
        function process_00(id, index) {
            layer.open({
                type:1,
                title:"流转",
                skin:"myclass",
                area:["500px","300px"],
                content:$("#process_00").html(),
                success:function (layero,index) {
                    var form = layui.form;
                    form.render();
                    layero.addClass('layui-form');
                    layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
                    getRepaireInfo(layero,'/repaireInfo/getRepaireinfo',id);


                },
                btn:['提交','关闭','完成'],
                yes:function (index,layero) {
                    var form = layui.form;
                    form.on('submit(fromContent)', function (data) {
                        var currentId = $(layero).find('select[name=currentId]').val();
                        var json = {"id":id,"type":currentId}
                        circulation(json,function () {
                            layer.close(index);
                            $('#list').datagrid('reload');
                        });
                    });
                },
                btn2:function (index) {
                    layer.close(index);
                },
                btn3:function (index) {
                    endServer(id,index)
                }
            });
        }

        function renderInfo(data,layero) {
            for(var key in data){
                if(data[key]!=null)
                    $(layero).find("#edit"+key).text(data[key]);
            }
            if(data["picAddress"] != null){
                layero.find('img[name=pic]').attr("src",ctx+"pic/getPic?fileId="+data["picAddress"]).attr("style","display:inline;width:150px;position:absolute;right:20px;top:20px;");
            }
            var form = layui.form;
            form.render();
        }
        function getRepaireInfo(layero,pash,id,orderId){
            $.ajax({
                url:ctx + pash,
                method:'get',
                data:{"id":id,"workOrderId":orderId},
                dataType:'json',
                async:false,
                success:function(result){
                    if(result.code=='0'){
                        renderInfo(result.data,layero);
                    }else{
                        var layer = layui.layer;
                        layer.open({
                            title: '报错了'
                            ,content: result.msg
                        });
                    }
                },
                error: function (result) {
                    var layer = layui.layer;
                    layer.open({
                        title: '报错了'
                        ,content: result.msg
                    });
                }
            });
        }

        function circulation(json,callback) {
            $.ajax({
                url:ctx+'/repaireInfo/execute/circulation',
                type : 'POST',
                data:json,
                success:function () {
                    if(callback !=null){
                        callback();
                    }

                }
            });
        }

        function handleCirculation(id,index){
            process_00(id,index);
        }

        function endServer(id) {
            $.ajax({
                url:ctx+'/repaireInfo/endServer/'+id,
                type : 'POST',
                success:function (result) {
                    var json = JSON.parse(result);
                    if(json.code=='0'){
                        $('#list').datagrid('reload');
                    }else{
                        layer.open({
                            title: '报错了'
                            ,content: json.msg
                        });
                    }
                }
            });
        }
    </script>

    <script type="text/javascript">
        // email新增
        function emailInsert(id,json) {
            layer.open({
                type: 1,
                title: "新增订单",
                skin: "myclass",
                area: ["500px", "500px"],
                content: $("#edit1").html(),
                success: function (layero, index) {
                    var form = layui.form;
                    layero.addClass('layui-form');
                    layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
                    $(layero).find('input[name=id]').val(json.id);
                    $(layero).find('input[name=creatorName]').val(json.creatorName);
                    $(layero).find('input[name=callerCompanyName]').val(json.callerCompanyName);
                    $(layero).find('input[name=callerDeptName]').val(json.callerDeptName);
                    $(layero).find('input[name=createMobile]').val(json.creatorPhone);
                    $(layero).find('input[name=description]').val(json.description);
                    $(layero).find('select[name=expectTime]').html(valExpectTime(json.expectTime));
                    $(layero).find('select[name=expectTime]').val(json.expectTime);
                    var _address = json.address.split("-")[0];
                    $(layero).find('select[name=address] option').each(function (i,n) {
                        if($(n).text()==_address) {
                            $(n).attr("selected", true);
                        }
                    });

                    form.on('select(addressFilter)', function(data){
                        selectfloor(layero)
                        form.render();
                    });
                    $(layero).find('select[name=floor]').val(json.floor);
                    $(layero).find('select[name=machineType]').html(valMachineType());
                    $(layero).find('select[name=machineType] option').each(function(i,n){
                        if($(n).text()==json.machineType) {
                            $(n).attr("selected",true);
                            changeSubject(layero);
                            $(layero).find('select[name=repaireSubject]').val(json.repaireSubject);
                        }
                    });
                    form.on('select(machineTypeFilter)', function(data){
                        changeSubject(layero);
                        form.render();
                    });
                    form.render();
                },
                btn: ['提交', '关闭'],
                yes:function (index,layero) {
                    var form = layui.form;
                    form.on('submit(fromContent)', function (data) {
                        var jsonData = $(layero).find("form").toJSON();
                        addEmail(jsonData,function () {
                            layer.close(index);
                        });
                    });
                },
                btn2:function (index) {
                    layer.close(index);
                }
            })
        }

        // 给服务时间赋值
        function valExpectTime(expectTime) {
            var htmlStr = "";
            if(expectTime =='上午' || expectTime == '下午'){
                htmlStr+="<option value='上午'>上午</option><option value='下午'>下午</option>";
            }else{
                for(var i=9;i<=17;i+=0.5){
                    var time;
                    if((i+'').endWith('.5')){
                        time  = (i-0.5)+':30  -  '+(i+0.5)+':00';
                    }else{
                        time = i+':00  -  '+i+':30';
                    }
                    htmlStr +="<option value='"+time+"'>"+time+"</option>";
                }
            }
            return htmlStr;
        }

        function addEmail(json,callback) {
            $.ajax({
                url:ctx+'/repaireInfo/add',
                type : 'POST',
                data:json,
                success:function () {
                    if(callback !=null){
                        callback();
                    }

                }
            });
        }

        function changeSubject(layero) {
            var _machineType = $(layero).find('select[name=machineType]').val();
            $.each(repaireSubject,function (name,value) {
                if(_machineType == value.value){
                    var htmlStr = "";
                    $.each(value.list,function(index,text){
                        htmlStr += "<option value='"+text.id+"'>"+text.words+"</option>";
                    });
                    $(layero).find("select[name=repaireSubject]").empty();
                    $(layero).find("select[name=repaireSubject]").html(htmlStr);
                    return false;
                }
            })
        }

        String.prototype.endWith=function(str){
            if(str==null||str==""||this.length==0||str.length>this.length)
                return false;
            if(this.substring(this.length-str.length)==str)
                return true;
            else
                return false;
            return true;
        }

        //  选择楼层
        function selectfloor(layero) {
            var _address = $(layero).find('select[name=address]').val();
            var $floor = $(layero).find("select[name=floor]");
            $floor.empty();
            var floors= floor_json[_address];
            if(floors == undefined) floors = "";
            var floor_html = "";
            $.each(floors,function (i,v) {
                floor_html +="<option value='"+v+"'>"+v+"</option>";
            });
            $floor.html(floor_html);
        }

        // 给分类赋值
        function valMachineType() {
            var htmlStr = "<option value=''>请选择...</option>";
            $.each(repaireSubject,function (name,value) {
                htmlStr += "<option value = '"+value.value+"'>"+value.text+"</option>";
            });
            return htmlStr;
        }
        $(function () {
            $.getJSON(ctx + '/picWord/listAll', function (result) {
                if (result.code == '0') {
                    repaireSubject = result.data;
                }
            });
        });
    </script>
</head>

<div class="layui-row" id="edit1" style="display: none;">
    <form id = "edit_form" class="layui-form layui-form-pane" action="">
        <input type="hidden" name="id"/>
        <input type="hidden" name="source" value="2"/>
        <div class="layui-form-item">
            <label class="layui-form-label">报修人</label>
            <div class="layui-input-inline">
                <input  id="creatorName"  name="creatorName" lay-verify="required" placeholder="请输入"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input  id="createMobile"  name="createMobile" lay-verify="required" placeholder="请输入"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <input  id="callerDeptName"  name="callerDeptName"  placeholder="请输入"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地点</label>
            <div class="layui-input-inline">
                <select name="address" lay-filter="addressFilter">
                    <option value="">---请选择---</option>
                    <option value="01">兆丰</option>
                    <option value="02">科汇</option>
                    <option value="03">百丽</option>
                    <option value="05">杨浦</option>
                    <option value="04">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">楼层</label>
            <div class="layui-input-inline">
                <select name="floor">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">维修时间</label>
            <div class="layui-input-inline">
                <select name="expectTime">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选项</label>
            <div class="layui-input-inline">
                <select class="weui-select" name="machineType" id="machineType" lay-filter="machineTypeFilter"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">判定常用</label>
            <div class="layui-input-inline">
                <select class="weui-select" name="repaireSubject" id="repaireSubject">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input name="description"  placeholder="请输入"  class="layui-input">
            </div>
        </div>
    </form>
</div>
<body>

    <div class="layui-row" id="process_00" style="display: none;">
        <form id = "circulation_form" class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label">来电号码</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editcreateMobile"></label>
                    </div>
                    <img src="" name="pic" alt="" style="display:none; width:150px;position:absolute;right:20px;top:20px;">
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">报修人</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editcreatorName"></label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editcallerDeptName"></label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">地点</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editaddress"></label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选项</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editmachineType"></label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">判定常用</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-label" id="editrepaireSubject"></label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <div class="layui-form-label" id="editdescription"></div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</body>
</html>
